# Copyright (C) 2008 Marcin Cieślik <marcin.cieslik@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
cimport numpy as np
ctypedef np.npy_double DTYPE_t

cdef enum constants:
    NSTACK = 100

cdef struct kdpoint:
    unsigned int index
    DTYPE_t *coords

cdef struct kdnode:
    unsigned int bucket                 # 1 if leaf-bucket, 0 if node
    int dimension
    DTYPE_t position
    unsigned int start, end             # start and end index of data points
    kdnode *left, *right                # pointers to left and right nodes

cdef inline void swap(kdpoint*, kdpoint*)

cdef kdpoint *points(DTYPE_t*, unsigned int, unsigned int)

cdef inline DTYPE_t dist(kdpoint*, kdpoint*, unsigned int)

cdef void qsort(kdpoint*, unsigned int, unsigned int, unsigned int)

cdef kdnode *build_tree(kdpoint*, unsigned int, unsigned int,\
                unsigned int, unsigned int, unsigned int)

cdef unsigned int rn(kdnode*, kdpoint*, kdpoint, DTYPE_t**,\
                unsigned int**, DTYPE_t, unsigned int, unsigned int)
                
cdef void *knn(kdnode*, kdpoint*, kdpoint, DTYPE_t*, unsigned int*, unsigned int, unsigned int)

cdef class kdtree:
    cdef np.ndarray n_array
    cdef DTYPE_t *c_array
    cdef kdpoint *kdpnts
    cdef kdnode *tree
    cdef public unsigned int bucket_size
    cdef unsigned int dims
    cdef unsigned int pnts
